package com.woniuxy.oldmanapi.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.OlderHeight;
import com.woniuxy.oldmanapi.entity.RechargeRecord;
import com.woniuxy.oldmanapi.service.CardService;
import com.woniuxy.oldmanapi.service.RecordService;
import com.woniuxy.oldmanapi.vo.RecordVo;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class RecordController {

    @Autowired
    RecordService recordService;
    @Autowired
    CardService cardService;

    @GetMapping("/record/list")
    public ResponseResult<PageInfo<RecordVo>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name) {
        ResponseResult<PageInfo<RecordVo>> result = new ResponseResult<>();
        try {
            PageInfo<RecordVo> lists = recordService.findRecordByPage(now, size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
    @PostMapping("/record/recharge")
    public ResponseResult<Void> modify(@RequestBody RechargeRecord record) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            record.setDate(new Date());
            recordService.recharge(record);
            cardService.updateBalance(record.getNum(),record.getCardId());
            result.setCode(200);
            result.setMsg("充值成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("充值失败!");
        }
        return result;
    }
}
